package com.bjsxt.userCenter.user.service.impl.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userCenter.common.util.ConstantFinalUtil;
import com.bjsxt.userCenter.common.util.PageUtil;
import com.bjsxt.userCenter.user.service.impl.dao.IAAdminsDao;
import com.bjsxt.userCenter.user.service.impl.dao.IARolesDao;
import com.bjsxt.userCenter.user.service.pojo.AAdmins;
import com.bjsxt.userCenter.user.service.pojo.ARoles;
import com.bjsxt.userCenter.user.service.service.IUserService;
@Service("userService")
public class IUserServiceImpl implements IUserService
{
	private List<AAdmins> adminsList = new ArrayList();
	private List<ARoles> rolesList = new ArrayList();
	@Resource
	IAAdminsDao adminDao;
	@Resource
	IARolesDao roleDao;
	@Override
	public JSONObject saveOneAAdmins(AAdmins admin)
	{
		int res = this.adminDao.save(admin);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		resultJSON.put("code", "1");
		resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("1"));
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAAdmins(AAdmins admin)
	{
		int res = this.adminDao.update(admin);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		resultJSON.put("code", "2");
		resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("2"));
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneAAdmins(AAdmins admin)
	{
		int res = this.adminDao.delete(admin);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		resultJSON.put("code", "3");
		resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("3"));
		return resultJSON;
	}

	@Override
	public AAdmins findOneAAdmins(Map<String, Object> condMap)
	{
		return this.adminDao.findOne(condMap);
	}

	@Override
	public List<AAdmins> findCondListAAdmins(Map<String, Object> condMap,PageUtil page)
	{
		if(condMap.get("keyWord")!=null && !"".equalsIgnoreCase(condMap.get("keyWord")+""))
		{
			condMap.put("keyWord", "%"+condMap.get("keyWord")+"%");
		}
		if(page != null)
		{
			condMap.put("pageCount", "true");
			AAdmins admin = (AAdmins) this.adminDao.findCondMap(condMap);
			page.setTotalRecord(admin.getId());
			condMap.put("pageCount", "false");
			condMap.put("currentPage", page.getCurrPage());
			condMap.put("pageSize", page.getSize());
		}
		this.adminsList = this.adminDao.findCondMap(condMap);
		return this.adminsList;
	}
	@Override
	public JSONObject saveOneARoles(ARoles role)
	{
		int res = this.roleDao.save(role);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		resultJSON.put("code", "1");
		resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("1"));
		return resultJSON;
	}

	@Override
	public JSONObject updateOneARoles(ARoles role)
	{
		int res = this.roleDao.update(role);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		resultJSON.put("code", "2");
		resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("2"));
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneARoles(ARoles role)
	{
		int res = this.roleDao.delete(role);
		JSONObject resultJSON = new JSONObject();
		if(res > 0)
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("0"));
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effect", res);
			resultJSON.put("data", dataJSON);
			return resultJSON;
		}
		resultJSON.put("code", "3");
		resultJSON.put("info", ConstantFinalUtil.codeInfoJSON.getString("3"));
		return resultJSON;
	}

	@Override
	public ARoles findOneARoles(Map<String, Object> condMap)
	{
		return this.roleDao.findOne(condMap);
	}

	@Override
	public List<ARoles> findCondListARoles(Map<String, Object> condMap,PageUtil page)
	{
		if(condMap.get("keyWord")!=null && !"".equalsIgnoreCase(condMap.get("keyWord")+""))
		{
			condMap.put("keyWord", "%"+condMap.get("keyWord")+"%");
		}
		if(page != null)
		{
			condMap.put("pageCount", "true");
			ARoles role = (ARoles) this.roleDao.findCondMap(condMap);
			page.setTotalRecord(role.getId());
			condMap.put("pageCount", "false");
			condMap.put("currentPage", page.getCurrPage());
			condMap.put("pageSize", page.getSize());
		}
		this.rolesList = this.roleDao.findCondMap(condMap);
		return this.rolesList;
	}


}
